package org.beian.fmmall.controller;




import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.beian.fmmall.VO.ResultVO;
import org.beian.fmmall.beans.ShoppingCart;
import org.beian.fmmall.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@CrossOrigin
@RequestMapping("/shopcart")
@Api(tags = "购物车管理",value = "购物车管理的接口")
public class ShopCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;
    private SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

    @ApiOperation(value = "添加购物车")
    @PostMapping ("/add")
    public ResultVO add(@RequestBody ShoppingCart cart){
        cart.setCartTime(sdf.format(new Date()));
        ResultVO resultVO = shoppingCartService.addShoppingCart(cart);
        System.out.println(resultVO);
        return resultVO;
    }
    @ApiOperation("购物车列表")
    @GetMapping("/list")
    @ApiImplicitParam(name = "userId",value = "用户ID",required = true)
    public ResultVO list(Integer userId,@RequestHeader("token") String token){
        return shoppingCartService.listShoppingCartByUserId(userId);
    }
    @ApiOperation("修改购物车数量")
    @PutMapping("/update/{cid}/{cnum}")
    public ResultVO update(@PathVariable("cid") int cid,
                           @PathVariable("cnum") int cnum){
        ResultVO resultVO = shoppingCartService.updataCartNum(cid, cnum);
        return resultVO;
    }
    @ApiOperation("删除购物车")
    @DeleteMapping("/delete/{cid}")
    public ResultVO delete(@PathVariable("cid") int cid){
        ResultVO resultVO = shoppingCartService.deleteCart(cid);
        return resultVO;
    }
    @GetMapping ("/listByCids")
    @ApiOperation("根据购物车ID查询购物车记录")
    @ApiImplicitParam(name = "cids",value = "选择的购物车记录ID",required = true)
    public ResultVO listByCids(String cids, @RequestHeader("token") String token){
        return shoppingCartService.listShoppingCartByCids(cids);
    }
}
